provider-config-modal.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useBoolean } from 'ahooks'
  6. import Field from './field'
  7. import type { LangFuseConfig, LangSmithConfig } from './type'
  8. import { TracingProvider } from './type'
  9. import { docURL } from './config'
  10. import {
  11. PortalToFollowElem,
  12. PortalToFollowElemContent,
  13. } from '@/app/components/base/portal-to-follow-elem'
  14. import { Lock01 } from '@/app/components/base/icons/src/vender/solid/security'
  15. import Button from '@/app/components/base/button'
  16. import { LinkExternal02 } from '@/app/components/base/icons/src/vender/line/general'
  17. import Confirm from '@/app/components/base/confirm'
  18. import { addTracingConfig, removeTracingConfig, updateTracingConfig } from '@/service/apps'
  19. import Toast from '@/app/components/base/toast'
  20. type Props = {
  21. appId: string
  22. type: TracingProvider
  23. payload?: LangSmithConfig | LangFuseConfig | null
  24. onRemoved: () => void
  25. onCancel: () => void
  26. onSaved: (payload: LangSmithConfig | LangFuseConfig) => void
  27. onChosen: (provider: TracingProvider) => void
  28. }
  29. const I18N_PREFIX = 'app.tracing.configProvider'
  30. const langSmithConfigTemplate = {
  31. api_key: '',
  32. project: '',
  33. endpoint: '',
  34. }
  35. const langFuseConfigTemplate = {
  36. public_key: '',
  37. secret_key: '',
  38. host: '',
  39. }
  40. const ProviderConfigModal: FC<Props> = ({
  41. appId,
  42. type,
  43. payload,
  44. onRemoved,
  45. onCancel,
  46. onSaved,
  47. onChosen,
  48. }) => {
  49. const { t } = useTranslation()
  50. const isEdit = !!payload
  51. const isAdd = !isEdit
  52. const [isSaving, setIsSaving] = useState(false)
  53. const [config, setConfig] = useState<LangSmithConfig | LangFuseConfig>((() => {
  54. if (isEdit)
  55. return payload
  56. if (type === TracingProvider.langSmith)
  57. return langSmithConfigTemplate
  58. return langFuseConfigTemplate
  59. })())
  60. const [isShowRemoveConfirm, {
  61. setTrue: showRemoveConfirm,
  62. setFalse: hideRemoveConfirm,
  63. }] = useBoolean(false)
  64. const handleRemove = useCallback(async () => {
  65. await removeTracingConfig({
  66. appId,
  67. provider: type,
  68. })
  69. Toast.notify({
  70. type: 'success',
  71. message: t('common.api.remove'),
  72. })
  73. onRemoved()
  74. hideRemoveConfirm()
  75. }, [hideRemoveConfirm, appId, type, t, onRemoved])
  76. const handleConfigChange = useCallback((key: string) => {
  77. return (value: string) => {
  78. setConfig({
  79. ...config,
  80. [key]: value,
  81. })
  82. }
  83. }, [config])
  84. const checkValid = useCallback(() => {
  85. let errorMessage = ''
  86. if (type === TracingProvider.langSmith) {
  87. const postData = config as LangSmithConfig
  88. if (!postData.api_key)
  89. errorMessage = t('common.errorMsg.fieldRequired', { field: 'API Key' })
  90. if (!errorMessage && !postData.project)
  91. errorMessage = t('common.errorMsg.fieldRequired', { field: t(`${I18N_PREFIX}.project`) })
  92. }
  93. if (type === TracingProvider.langfuse) {
  94. const postData = config as LangFuseConfig
  95. if (!errorMessage && !postData.secret_key)
  96. errorMessage = t('common.errorMsg.fieldRequired', { field: t(`${I18N_PREFIX}.secretKey`) })
  97. if (!errorMessage && !postData.public_key)
  98. errorMessage = t('common.errorMsg.fieldRequired', { field: t(`${I18N_PREFIX}.publicKey`) })
  99. if (!errorMessage && !postData.host)
  100. errorMessage = t('common.errorMsg.fieldRequired', { field: 'Host' })
  101. }
  102. return errorMessage
  103. }, [config, t, type])
  104. const handleSave = useCallback(async () => {
  105. if (isSaving)
  106. return
  107. const errorMessage = checkValid()
  108. if (errorMessage) {
  109. Toast.notify({
  110. type: 'error',
  111. message: errorMessage,
  112. })
  113. return
  114. }
  115. const action = isEdit ? updateTracingConfig : addTracingConfig
  116. try {
  117. await action({
  118. appId,
  119. body: {
  120. tracing_provider: type,
  121. tracing_config: config,
  122. },
  123. })
  124. Toast.notify({
  125. type: 'success',
  126. message: t('common.api.success'),
  127. })
  128. onSaved(config)
  129. if (isAdd)
  130. onChosen(type)
  131. }
  132. finally {
  133. setIsSaving(false)
  134. }
  135. }, [appId, checkValid, config, isAdd, isEdit, isSaving, onChosen, onSaved, t, type])
  136. return (
  137. <>
  138. {!isShowRemoveConfirm
  139. ? (
  140. <PortalToFollowElem open>
  141. <PortalToFollowElemContent className='w-full h-full z-[60]'>
  142. <div className='fixed inset-0 flex items-center justify-center bg-black/[.25]'>
  143. <div className='mx-2 w-[640px] max-h-[calc(100vh-120px)] bg-white shadow-xl rounded-2xl overflow-y-auto'>
  144. <div className='px-8 pt-8'>
  145. <div className='flex justify-between items-center mb-4'>
  146. <div className='text-xl font-semibold text-gray-900'>{t(`${I18N_PREFIX}.title`)}{t(`app.tracing.${type}.title`)}</div>
  147. </div>
  148. <div className='space-y-4'>
  149. {type === TracingProvider.langSmith && (
  150. <>
  151. <Field
  152. label='API Key'
  153. labelClassName='!text-sm'
  154. isRequired
  155. value={(config as LangSmithConfig).api_key}
  156. onChange={handleConfigChange('api_key')}
  157. placeholder={t(`${I18N_PREFIX}.placeholder`, { key: 'API Key' })!}
  158. />
  159. <Field
  160. label={t(`${I18N_PREFIX}.project`)!}
  161. labelClassName='!text-sm'
  162. isRequired
  163. value={(config as LangSmithConfig).project}
  164. onChange={handleConfigChange('project')}
  165. placeholder={t(`${I18N_PREFIX}.placeholder`, { key: t(`${I18N_PREFIX}.project`) })!}
  166. />
  167. <Field
  168. label='Endpoint'
  169. labelClassName='!text-sm'
  170. value={(config as LangSmithConfig).endpoint}
  171. onChange={handleConfigChange('endpoint')}
  172. placeholder={'https://api.smith.langchain.com'}
  173. />
  174. </>
  175. )}
  176. {type === TracingProvider.langfuse && (
  177. <>
  178. <Field
  179. label={t(`${I18N_PREFIX}.secretKey`)!}
  180. labelClassName='!text-sm'
  181. value={(config as LangFuseConfig).secret_key}
  182. isRequired
  183. onChange={handleConfigChange('secret_key')}
  184. placeholder={t(`${I18N_PREFIX}.placeholder`, { key: t(`${I18N_PREFIX}.secretKey`) })!}
  185. />
  186. <Field
  187. label={t(`${I18N_PREFIX}.publicKey`)!}
  188. labelClassName='!text-sm'
  189. isRequired
  190. value={(config as LangFuseConfig).public_key}
  191. onChange={handleConfigChange('public_key')}
  192. placeholder={t(`${I18N_PREFIX}.placeholder`, { key: t(`${I18N_PREFIX}.publicKey`) })!}
  193. />
  194. <Field
  195. label='Host'
  196. labelClassName='!text-sm'
  197. isRequired
  198. value={(config as LangFuseConfig).host}
  199. onChange={handleConfigChange('host')}
  200. placeholder='https://cloud.langfuse.com'
  201. />
  202. </>
  203. )}
  204. </div>
  205. <div className='my-8 flex justify-between items-center h-8'>
  206. <a
  207. className='flex items-center space-x-1 leading-[18px] text-xs font-normal text-[#155EEF]'
  208. target='_blank'
  209. href={docURL[type]}
  210. >
  211. <span>{t(`${I18N_PREFIX}.viewDocsLink`, { key: t(`app.tracing.${type}.title`) })}</span>
  212. <LinkExternal02 className='w-3 h-3' />
  213. </a>
  214. <div className='flex items-center'>
  215. {isEdit && (
  216. <>
  217. <Button
  218. className='h-9 text-sm font-medium text-gray-700'
  219. onClick={showRemoveConfirm}
  220. >
  221. <span className='text-[#D92D20]'>{t('common.operation.remove')}</span>
  222. </Button>
  223. <div className='mx-3 w-px h-[18px] bg-gray-200'></div>
  224. </>
  225. )}
  226. <Button
  227. className='mr-2 h-9 text-sm font-medium text-gray-700'
  228. onClick={onCancel}
  229. >
  230. {t('common.operation.cancel')}
  231. </Button>
  232. <Button
  233. className='h-9 text-sm font-medium'
  234. variant='primary'
  235. onClick={handleSave}
  236. loading={isSaving}
  237. >
  238. {t(`common.operation.${isAdd ? 'saveAndEnable' : 'save'}`)}
  239. </Button>
  240. </div>
  241. </div>
  242. </div>
  243. <div className='border-t-[0.5px] border-t-black/5'>
  244. <div className='flex justify-center items-center py-3 bg-gray-50 text-xs text-gray-500'>
  245. <Lock01 className='mr-1 w-3 h-3 text-gray-500' />
  246. {t('common.modelProvider.encrypted.front')}
  247. <a
  248. className='text-primary-600 mx-1'
  249. target='_blank' rel='noopener noreferrer'
  250. href='https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html'
  251. >
  252. PKCS1_OAEP
  253. </a>
  254. {t('common.modelProvider.encrypted.back')}
  255. </div>
  256. </div>
  257. </div>
  258. </div>
  259. </PortalToFollowElemContent>
  260. </PortalToFollowElem>
  261. )
  262. : (
  263. <Confirm
  264. isShow
  265. type='warning'
  266. title={t(`${I18N_PREFIX}.removeConfirmTitle`, { key: t(`app.tracing.${type}.title`) })!}
  267. content={t(`${I18N_PREFIX}.removeConfirmContent`)}
  268. onConfirm={handleRemove}
  269. onCancel={hideRemoveConfirm}
  270. />
  271. )}
  272. </>
  273. )
  274. }
  275. export default React.memo(ProviderConfigModal)